﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCodeSolutions
{
    /// <summary>
    /// 1006.笨阶乘
    /// </summary>
    class LeetCode1006: LeetCodeBase
    {
        public override void Execute()
        {
            CommonFuntions.Print(Clumsy(10));
        }

        // 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1
        // 12 = (10 * 9 / 8) + 7 - (6 * 5 / 4) + 3 - (2 * 1)

        // (9 * 8 / 7 + 6) - (5 * 4 / 3 + 2) - 1

        public int Clumsy(int num)
        {
            string questionStr = "";
            for (int i = num; i > 0; i--)
            {
                string ope = GetOperate();
                questionStr += i + ope;
            }
            questionStr = questionStr.Substring(0, questionStr.Length - 1); //拼接出笨阶乘公式

            string[] splitStrs = questionStr.Split('-');
            List<int> partResultsList = new List<int>();
            for (int i = 0; i < splitStrs.Length; i++)
            {
                //CommonFuntions.Print(splitStrs[i]);

                int partResult = GetPartResult(splitStrs[i], i == 0);
                partResultsList.Add(partResult);
                //CommonFuntions.Print("res " + partResult);
            }

            if (partResultsList.Count == 1)
            {
                //CommonFuntions.Print(partResultsList[0]);

                return partResultsList[0];
            }

            int result = partResultsList[0];
            for (int i = 1; i < partResultsList.Count; i++)
            {
                result -= partResultsList[i];
            }

            return result;
        }

        public string[] operateList = new string[] { "*", "/", "+", "-" };
        public int operateIndex = 0;

        public int GetPartResult(string part, bool isFirst)
        {
            int result = 0;

            string[] splits = new string[] { "*", "/", "+" };
            string[] numStrs = part.Split(splits, StringSplitOptions.RemoveEmptyEntries);

            List<int> numsList = new List<int>();
            foreach (var numStr in numStrs)
            {
                numsList.Add(int.Parse(numStr)); //所有的数字
            }

            result = numsList[0];

            if (numsList.Count == 1)
            {
                return result;
            }

            for (int i = 1; i < numsList.Count; i++)
            {
                if (i == 1)
                {
                    result *= numsList[i];
                }
                else if (i == 2)
                {
                    result /= numsList[i];
                }
                else if (i == 3)
                {
                    if (isFirst)
                    {
                        result += numsList[i];
                    }
                    else
                    {
                        result -= numsList[i];
                    }
                }
            }

            return result;
        }

        public string GetOperate()
        {
            string result = operateList[operateIndex];

            operateIndex++;
            operateIndex %= operateList.Length;

            return result;
        }
    }
}
